<template>
  <view class="edit-address">
    <view class="header">
      <text class="title">编辑地址</text>
    </view>
    
    <view class="form-container">
      <view class="form-item">
        <text class="form-label">所在地区：</text>
        <input 
          class="form-input" 
          placeholder="省/市/区" 
          v-model="formData.region" 
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">详细地址：</text>
        <input 
          class="form-input" 
          placeholder="街道、小区、大厦或学校" 
          v-model="formData.address" 
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">门牌号：</text>
        <input 
          class="form-input" 
          placeholder="楼号-门牌号" 
          v-model="formData.doorNumber" 
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">收货人：</text>
        <input 
          class="form-input" 
          placeholder="请输入收货人姓名" 
          v-model="formData.consignee" 
        />
      </view>
      
      <view class="form-item">
        <text class="form-label">联系电话：</text>
        <input 
          class="form-input"
          type="number"
          placeholder="请输入手机号码" 
          v-model="formData.phone" 
        />
      </view>
    </view>
    
    <button class="submit-btn" @click="saveAddress">保存修改</button>
  </view>
</template>

<script>
	import config from "@/Config.js";
export default {
  data() {
    return {
      id: "",
      formData: {
        region: "",
        address: "",
        doorNumber: "",
        consignee: "",
        phone: ""
      }
    }
  },
  onLoad(options) {
    this.id = options.id;
    this.query();
  },
  methods: {
    query() {
      uni.showLoading({
        title: '加载中...'
      });
      
      uni.request({
        url: config.config+`/address?id=${this.id}`,
        method: 'GET',
        success: (res) => {
			const token = uni.getStorageSync('token')
          uni.hideLoading();
          if (res.data && res.data.length > 0) {
            const addressData = res.data[0];
            this.formData = {
              region: addressData.region || "",
              address: addressData.address || "",
              doorNumber: addressData.doorNumber || "",
              consignee: addressData.consignee || "",
              phone: addressData.phone || "",
			  token:token
            };
          }
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showToast({
            title: '加载失败',
            icon: 'none'
          });
          console.error('请求失败:', err);
        }
      });
    },
saveAddress() {
  uni.showLoading({ title: '保存中...' });
  
  // 确保使用 PUT 方法
  uni.request({
    url: `${config.config}/address/${this.id}`, // 使用模板字符串
    method: 'PUT', // 明确指定方法
    header: {
      'Content-Type': 'application/json'
    },
    data: this.formData,
    success: (res) => {
      console.log('保存响应:', {
        status: res.statusCode,
        data: res.data
      });
      
      if (res.statusCode === 200) {
        uni.showToast({ title: '修改成功' });
        setTimeout(() => uni.navigateBack(), 1500);
      } else {
        uni.showToast({ 
          title: `保存失败: ${res.data?.message || '未知错误'}`,
          icon: 'none' 
        });
      }
    },
    fail: (err) => {
      console.error('请求失败详情:', {
        errMsg: err.errMsg,
        errObj: err
      });
      uni.showToast({ 
        title: '网络请求失败',
        icon: 'none'
      });
    },
    complete: () => uni.hideLoading()
  });
}
  }
}
</script>

<style>
.edit-address {
  padding: 30rpx;
}

.header {
  margin-bottom: 40rpx;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
  color: #333;
}

.form-container {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 30rpx;
}

.form-item {
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
}

.form-label {
  width: 160rpx;
  font-size: 28rpx;
  color: #666;
}

.form-input {
  flex: 1;
  height: 80rpx;
  padding: 0 20rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  font-size: 28rpx;
}

.submit-btn {
  background-color: #07c160;
  color: #fff;
  border: none;
  border-radius: 50rpx;
  height: 90rpx;
  line-height: 90rpx;
  font-size: 32rpx;
}
</style>